package com.mrhc.android;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.device.DeviceCallback;
import com.device.geit.GeitDevice;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class TestActivity extends AppCompatActivity implements View.OnClickListener {
    private final static int REQUEST_PERMISSIONS = 1000;
    private Button btn_connect,btn_idcard1,btn_idcard2,btn_idcard3,btn_idcard4,btn_idcard5,btn_init;
    private GeitDevice geitDevice;
    private Context mContext;


    Handler mHandler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            int what = msg.what;
            if (what==4){
                //读取身份证成功
//                runOnUiThread();
                JSONObject jsonObject= (JSONObject) msg.obj;
                try {
                    Toast.makeText(mContext, "身份证读取成功"+jsonObject.getString("fullName"), Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }else if (what==8){
                String data= (String) msg.obj;
                Toast.makeText(mContext, "银行卡成功 卡号"+data, Toast.LENGTH_SHORT).show();

            }else if (what==14){
                String message=msg.obj.toString();
                Toast.makeText(mContext, "身份证银行卡失败 14："+message, Toast.LENGTH_SHORT).show();

            }else if (what==5){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject jsonObject= (JSONObject) msg.obj;
                        try {
                            Toast.makeText(mContext, "身份证读取成功 姓名"+jsonObject.getString("fullName"), Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });

            }


        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        checkPermissions(this);
        geitDevice=new GeitDevice();
        mContext=this;
        init();

        initclick();

        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }else {
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);

            // 下面两句是针对ActionBar的 如果主题是使用了NoActionBar,使用下面的是会报空指针异常的
                getSupportActionBar().hide();
        }


    }

    private void checkPermissions(TestActivity activity) {

        final List<String> permissionsList = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if ((activity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.READ_PHONE_STATE);
            if ((activity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);
            if ((activity.checkSelfPermission(Manifest.permission.MOUNT_FORMAT_FILESYSTEMS) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
            if ((activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if ((activity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            if ((activity.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.CAMERA);
            if (permissionsList.size() != 0) {
                activity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                        REQUEST_PERMISSIONS);
            }
        }
    }

    private void initclick() {

        btn_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                geitDevice.connectDevice();
                Toast.makeText(TestActivity.this, "连接设备调用成功", Toast.LENGTH_SHORT).show();


            }
        });
        btn_init.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                geitDevice.initDevice();
                Toast.makeText(TestActivity.this, "初始化设备成功", Toast.LENGTH_SHORT).show();

            }
        });
        btn_idcard1.setOnClickListener(this);
        btn_idcard2.setOnClickListener(this);
        btn_idcard3.setOnClickListener(this);
        btn_idcard4.setOnClickListener(this);
        btn_idcard5.setOnClickListener(this);


    }

    private void init() {

        btn_init=findViewById(R.id.btn_init);

        btn_connect=findViewById(R.id.btn_connect);
        btn_idcard1=findViewById(R.id.btn_idcard1);
        btn_idcard2=findViewById(R.id.btn_idcard2);
        btn_idcard3=findViewById(R.id.btn_idcard3);
        btn_idcard4=findViewById(R.id.btn_idcard4);
        btn_idcard5=findViewById(R.id.btn_idcard5);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.btn_idcard1:

                //第一种调用方式
                geitDevice.isConnDevice();
                geitDevice.readIDCardInfo("0", new DeviceCallback() {
                    @Override
                    public void onSuccess(JSONObject jsonObject) {

                        Message msg = new Message();
                        msg.what = 4;
                        msg.obj = jsonObject;
                        mHandler.sendMessage(msg);

                    }

                    @Override
                    public void onFail(JSONObject jsonObject) {

//                        try {
//                            Thread.sleep(8000);
//
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }

//                        startActivity(new Intent(TestActivity.this,CeshiActivity.class));
                        Message msg = new Message();
                        msg.what = 14;
                        msg.obj = jsonObject.toString();
                        mHandler.sendMessage(msg);
                    }
                },20);

                break;
            case R.id.btn_idcard2:
                geitDevice.readIDCardInfo("0", new DeviceCallback() {
                    @Override
                    public void onSuccess(JSONObject jsonObject) {

                        Message msg = new Message();
                        msg.what = 5;
                        msg.obj = jsonObject;
                        mHandler.sendMessage(msg);

                    }

                    @Override
                    public void onFail(JSONObject jsonObject) {
                        Message msg = new Message();
                        msg.what = 14;
                        msg.obj = jsonObject.toString();
                        mHandler.sendMessage(msg);
                    }
                },5);
                break;
            case R.id.btn_idcard3:
                geitDevice.readIDCardInfo("0", new DeviceCallback() {
                    @Override
                    public void onSuccess(JSONObject jsonObject) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                try {
                                    Toast.makeText(mContext, "身份证读取成功 姓名"+jsonObject.getString("fullName"), Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });

                    }

                    @Override
                    public void onFail(JSONObject jsonObject) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                    Toast.makeText(mContext, "身份证读取失败 姓名"+jsonObject.toString(), Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                },5);
                break;
            case R.id.btn_idcard4:

                geitDevice.readIDCardInfo("0", new DeviceCallback() {
                    @Override
                    public void onSuccess(JSONObject jsonObject) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                try {
                                    Toast.makeText(mContext, "身份证读取成功 姓名"+jsonObject.getString("fullName"), Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });

                    }

                    @Override
                    public void onFail(JSONObject jsonObject) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                Toast.makeText(mContext, "身份证读取失败 姓名"+jsonObject.toString(), Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                },10);

                break;
            case R.id.btn_idcard5:

                geitDevice.readIDCardInfo("0", new DeviceCallback() {
                    @Override
                    public void onSuccess(JSONObject jsonObject) {

                        Message msg = new Message();
                        msg.what = 5;
                        msg.obj = jsonObject;
                        mHandler.sendMessage(msg);

                    }

                    @Override
                    public void onFail(JSONObject jsonObject) {
                        Message msg = new Message();
                        msg.what = 14;
                        msg.obj = jsonObject.toString();
                        mHandler.sendMessage(msg);
                    }
                },10);

                break;



        }


    }
}
